package com.tianhui.consignortwofei.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.fgs.common.CommonDialog;
import com.fgs.common.utils.ToastUtil;
import com.tianhui.consignortwofei.R;
import com.fgs.common.utils.ImageUtil;


/**
 * @author fgs
 * @date 2020/4/9
 * @description
 **/
public class QRCodeDialog extends CommonDialog {
    private TextView positiveTextView;
    private TextView negativeTextView;

    private ImageView qrCodeImageView;
    private Bitmap bitmap;

    public QRCodeDialog(@NonNull Context context, Bitmap bitmap) {
        super(context, R.style.style_select_dialog);
        this.bitmap = bitmap;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_order_qr_code);

        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);

        setCancelable(true);
        setCanceledOnTouchOutside(false);

        positiveTextView = findViewById(R.id.layout_operate_tip_dialog_positiveTextView);
        negativeTextView = findViewById(R.id.layout_operate_tip_dialog_negativeTextView);

        qrCodeImageView = findViewById(R.id.dialog_order_qr_code_qrCodeImageView);
        qrCodeImageView.setImageBitmap(bitmap);

        positiveTextView.setText("保存");
        positiveTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();

                try {
                    ImageUtil.saveImageToGallery(getContext(), bitmap);
                    ToastUtil.showShortToast("保存成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtil.showShortToast("保存失败");
                }
            }
        });
        negativeTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

}
